package com.geostar.constructionland.analyse.service.handler.impl;



import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultDzzhyfq;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.service.analyseresult.AlyResultDzzhyfqService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import com.geostar.constructionland.common.dao.YzGeoJsonDao;
import com.geostar.constructionland.common.entity.logs.mongolog.YzGeoJson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

@Component
@Slf4j
public class DzzhyfHandler implements IResultHandler {

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    AlyResultDzzhyfqService dzzhyfqService;

    @Autowired
    private YzGeoJsonDao yzGeoJsonDao;
    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        log.info("处理地质灾害易发区分析服务，状态码，{}",httpResult.getCode());
        HandlerResult result = HandlerResult.createBySuccess();
        if(HttpStatus.SC_OK == httpResult.getCode()){
            /**解析xml格式，获取json数据**/
//            String newBody = XmlUtil.parseXml(httpResult.getBody());
//            httpResult.setBody(httpResult.getBody());
            /**保存占压信息**/
            AlyResult alyResult =objectMapper.readValue(httpResult.getBody(), AlyResult.class);
            log.info("body",httpResult.getBody());
            if(HttpStatus.SC_OK==alyResult.getStatus()){
                List<AlyResultDzzhyfq> dzzhyfqs = objectMapper.readValue(alyResult.getResults().getResultRecords(), new TypeReference<List<AlyResultDzzhyfq>>() {});
                /***如果不为空，存入数据库*/
                if(CollectionUtils.isNotEmpty(dzzhyfqs)){
                    for(AlyResultDzzhyfq alyResultDzzhyfq:dzzhyfqs){
                        //单独处理数据至mongo
                        YzGeoJson yzGeoJson = new YzGeoJson();
                        String yzGeoJsonId= UUID.randomUUID().toString().replaceAll("-","");
                        yzGeoJson.setId(yzGeoJsonId);
                        yzGeoJson.setGeoJson(alyResultDzzhyfq.getYzwkt());
                        yzGeoJsonDao.saveYzGeoJson(yzGeoJson);
                        //增加冗余数据
                        alyResultDzzhyfq.setMongoId(yzGeoJsonId);
                        alyResultDzzhyfq.setFxbs(analyseModel.getAlyId());
                    }
                    dzzhyfqService.saveBatch(dzzhyfqs);
                }
            }else {
                throw new Exception(alyResult.getMessage());
            }
        }
        return result;
    }
}
